package shared;

import java.util.ArrayList;
import java.util.LinkedList;

import fisica.FContactResult;

import processing.core.PApplet;
import processing.core.PShape;

import birdsVSZombiesClient.gameGraphic.BVZGameShapeBoundaries;
import birdsVSZombiesClient.gameGraphic.BVZGameShapeBoxes;
import birdsVSZombiesClient.gameGraphic.BVZGameShapeCircles;
import birdsVSZombiesComponents.AbstractBox2DShape;
import birdsVSZombiesComponents.BaseBox;
import birdsVSZombiesComponents.BaseCircle;
import birdsVSZombiesComponents.Birds;
import birdsVSZombiesComponents.BoundaryBox;
import birdsVSZombiesComponents.Zombies;


/**
 * The data sent from the Server to Clients.
 * <p>Use LinkedList to describe the physical items in the scene.<br/>
 * The message will be serialized into Json in the server and then send to the clients.<br/>
 * In clients the message will be deserialized back to message object by Gson.
 * @author yangliu
 * @version Version 0.0.4
 */
public class BVZServerToClientsMessages {
	
	public static PApplet parent;
	
	//	Server state
	public int ss;
	
	public int scon;
	
//	public PShape birdtexture;
	
	public LinkedList<BVZGameShapeBoxes> blocks = new LinkedList<BVZGameShapeBoxes>();
	public LinkedList<BVZGameShapeCircles> birds = new LinkedList<BVZGameShapeCircles>();
	public LinkedList<BVZGameShapeBoundaries> boundaries = new LinkedList<BVZGameShapeBoundaries>();
	
	public LinkedList<BVZGameShapeCircles> zombies = new LinkedList<BVZGameShapeCircles>();
	
	/**
	 * Pull the information from the Physical items and put them into the messages.
	 * @param ss 			The ServerStat.
	 * @param _boundaries	The boundaries boxes in the scene.
	 * @param _birds		The circles in the scene.
	 * @param _blocks			The boxes in the scene.
	 */
	public BVZServerToClientsMessages(int scon, int ss, ArrayList<BoundaryBox> _boundaries, ArrayList<Birds> _birds, ArrayList<Zombies> _zombies, ArrayList<BaseBox> _blocks) {
		this.ss = ss;
		this.scon = scon;
		if(_boundaries != null && _birds != null && _blocks != null){
			//	Clients don't need to draw the boundaries.
/*			for(int i=0; i<boundaries.size(); i++){
				BoundaryBox b = boundaries.get(i);
				
				BVZGameShapeBoundaries sb = new BVZGameShapeBoundaries(b.getPos().x, b.getPos().y, b.baseWidth, 
						b.baseHeight, b.getAngle(), b.baseColor, b.baseStrokeColor, b.baseStrokeWeight);
				this.mby.add(sb);
			}
*/			
			//	Add the circles items into the message
			for(int i=0; i<_birds.size(); i++){
				int largeContactNum = 0;
				Birds c = _birds.get(i);
				
				//	See if the message is from the contactStarted.
				if(scon == 1){
					for(int j=0; j<c.baseCircle.getContactsResults().size(); j++){
						if(((FContactResult) c.baseCircle.getContactsResults().get(j)).getNormalImpulse() > 4000){
							largeContactNum++;
						}
					}
				}
				
				BVZGameShapeCircles sc = new BVZGameShapeCircles(c.baseType, c.getPosX(), c.getPosY(), c.baseRadius, 
						c.baseRadius, c.getAngle(), c.baseColor, c.baseStrokeColor, c.baseStrokeWeight, largeContactNum, c.currentCollisionNum);
				
				this.birds.add(sc);
			}
			
			for(int i=0; i<_zombies.size(); i++){
				int largeContactNum = 0;
				Zombies z = _zombies.get(i);
				
				//	See if the message is from the contactStarted.
				if(scon == 1){
					for(int j=0; j<z.baseCircle.getContactsResults().size(); j++){
						if(((FContactResult) z.baseCircle.getContactsResults().get(j)).getNormalImpulse() > 4000){
							largeContactNum++;
						}
					}
				}
				
				BVZGameShapeCircles sz = new BVZGameShapeCircles(z.baseType, z.getPosX(), z.getPosY(), z.baseRadius, 
						z.baseRadius, z.getAngle(), z.baseColor, z.baseStrokeColor, z.baseStrokeWeight, largeContactNum, z.currentCollisionNum);
				
				this.zombies.add(sz);
			}

			
			//	Add the boxes items into the message
			for(int i=0; i<_blocks.size(); i++){
				int largeContactNum = 0;
				BaseBox b = _blocks.get(i);
				for(int j=0; j<b.baseBox.getContactsResults().size(); j++){
					if(((FContactResult) b.baseBox.getContactsResults().get(j)).getNormalImpulse() > 4000){
						largeContactNum++;
					}
				}
				BVZGameShapeBoxes sb = new BVZGameShapeBoxes(b.baseType, b.getPosX(), b.getPosY(), b.baseWidth,
						b.baseHeight, b.getAngle(), b.baseColor, b.baseStrokeColor, b.baseStrokeWeight, largeContactNum, b.currentCollisionNum, b.maxCollisionNum);
				this.blocks.add(sb);
			}

		}
	}
	
	public BVZServerToClientsMessages(int ss) {
		this(0,ss,null,null,null,null);
	}
	
	public BVZServerToClientsMessages() {
		this(0,1,null,null,null,null);
	}
}
